package com.ConfigServer.environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;

import com.ConfigServer.service.ConfigRepositoryService;

public class EnvironmentRepositoryImpl implements EnvironmentRepository{
	/*
	 * 用于标识配置信息从哪里取，如database代表从数据库中取，properties代表从配置文件中取
	 * 为null代表从配置文件和数据库中取，该属性是自己定义的
	 */
	@Value("${spring.cloud.config.server.configSource}")
	private String configSource;
	@Autowired
	private ConfigRepositoryService configRepositoryService;
	/*
	 * 其它服务启动的时该方法会被调用，从而获得属于自己的配置信息
	 */
	@Override
	public Environment findOne(String application, String profile, String label) {
		switch (configSource) {
		case "database":
			return configRepositoryService.getEnvironmentFromDatabaseByMybatis(application, profile, label);
		case "properties":
			return configRepositoryService.getEnvironmentFromProperties(application, profile, label);
		default:
			// TODO 可以把数据库和配置文件并集
			return null;
		}
	}
}
